<template>
  <div>
    <van-nav-bar
      title="账号登陆"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="from.username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="from.password"
        type="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button
          round
          block
          type="info"
          native-type="submit"
        >登入</van-button>
      </div>
    </van-form>
    <router-link to="/register" tag="a">还没有账号，去注册</router-link>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { login } from '@/api/user.js'
export default {
  data() {
    return {
      from: {
        username: '',
        password: ''
      }
    }
  },

  created() {

  },

  methods: {
    onClickLeft() {
      this.$router.back()
    },
    async onSubmit() {
      // console.log(this.from.username)
      try {
        const res = await login(this.from)
        console.log(res.data.body)
        this.$store.commit('initUser', res.data.body)
        this.$router.push('/layout/my')
      } catch (err) {
        Toast.fail('sb')
        // console.log(123)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.van-nav-bar{
  background-color: rgb(33, 185, 122) ;
}
/deep/.van-nav-bar__title{
  color: #fff;
}
/deep/.van-nav-bar__arrow{
  color: #fff !important;
}
</style>
